home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Programming / Programming Tools / Pascal Demos from Apple / scroll example / SCROLL.C.TEXT next >
Encoding:
Text File  |  1985-02-05  |  15.6 KB  |  361 lines  |  [TEXT/ttxt]

  1. /* ----------------------------------------------------------------------------------
  2. NAME
  3.      Scroll
  4.  
  5. DESCRIPTION
  6.       This code demonstrates how to use scroll bars.
  7.       You can scroll text or graphics or both.
  8.       You can scroll horizontally or vertically.
  9.  
  10. AUTHOR
  11.       Cary Clark, Macintosh Technical Support
  12.       Copyright 1984 Apple Computer Inc.
  13.  
  14. ---------------------------------------------------------------------------------- */
  15.  
  16. #include "mac/c/include/interface.h"   /* Macintosh definitions and functions */
  17.  
  18. #define Horizontal    1    /* These are the choices in the menu "Scroll Bar". */
  19. #define Vertical      2
  20. #define TextItem      4
  21. #define Graphics      5
  22.  
  23. #define FileMenu      1    /* resource numbers and position in the Menu bar */
  24. #define ScrollMenu    2
  25.  
  26. #define NumOfRects   30    /* quantity of rectangles and strings to scroll around */
  27. #define NumOfStrings 55
  28.  
  29. /* The graphics scrolled in this example are an array of rectangles. */
  30. typedef Rect MyRectData[NumOfRects], (**MyRectHdl)[NumOfRects]; /*  */
  31.  
  32.  
  33. TEHandle       hTE;                /* TextEdit handle */
  34. ControlHandle  hScroll,            /* horizontal scroll bar */
  35.                vScroll;            /* vertical scroll bar */
  36. WindowPtr      myWindow;           /* document window */
  37. MenuHandle     hdlScrollMenu;      /* handle to the menu items */
  38. MyRectHdl      myRect;             /* handle to array of rectangles */
  39. short          originalPart;       /* 1st part of the scroll bar hit */
  40. Point          pageCorner,         /* location of the upper left hand page corner */
  41.                eventPoint;         /* where an event took place */
  42. Rect           growBoxRect,        /* area of the window reserved for the grow box */
  43.                myViewRect;         /* display rectangle containing scrollable data */
  44. Boolean        doneFlag = false,   /* set true when the user selects 'Quit' */
  45.                showText = true,    /* set true when text can be scrolled */
  46.                showGraphics = false;   /* set true when graphics can be scrolled */
  47.  
  48. SetUpData() /* ------------------------------------------------------------------- */
  49.  
  50. /* This procedure initializes two data structures; a TextEdit record and an array of
  51. rectangles.  Initially, only text and the vertical scrollbar will be displayed. */
  52. {
  53.    StringHandle myString; /* temporary container for a string in the resource fork */
  54.    short counter;         /* counters must be local to the procedure */
  55.    Rect destRect;         /* rectangle containing the larger-than-the-screen page */
  56.  
  57.  
  58. /* The TextEdit record is initialized by reading in a string from the application's
  59. resource fork and then inserting it a number of times into the TextEdit record. */
  60.  
  61.    myString = GetString(256); /* Get some text to play around with. */
  62.  
  63. /* Set the view as the portrect less the vertical scrollbar area.  The TextEdit
  64. destRect will be set to the current window width plus an arbitrary value. */
  65.  
  66.    myViewRect = myWindow->portRect;
  67.    destRect = myViewRect;
  68.    destRect.right += 300;
  69.    pageCorner.h = -destRect.left;
  70.    pageCorner.v = -destRect.top;
  71.    myViewRect.right -= 15; /* subtract width of scrollbar */
  72.    hTE = TENew(&destRect, &myViewRect);
  73.  
  74.    HLock(myString);       /* can't move if we are going to point to the text */
  75. /* Create a TextEdit record full of the string. */
  76.    for (counter = 1; counter <= NumOfStrings; ++counter)
  77.       TEInsert(&(**myString).text[0], (**myString).length, hTE);
  78.    HUnLock(myString);    /* free to move again */
  79.  
  80. /* Now, create a structure of rectangles. */
  81.    myRect = (MyRectHdl) NewHandle(sizeof(MyRectData));  /* 240 bytes */
  82.    for (counter = 1; counter <= NumOfRects; ++counter)
  83.       SetRect(&(**myRect)[counter - 1], counter * 23, counter * 20, counter * 23 + 50,
  84.        counter * 20 + 50);
  85.  
  86.    ShowWindow(myWindow); /* Display the window and the text it contains */
  87.  
  88.    vScroll = GetNewControl(256, myWindow); /* vertical scrollbar */
  89.    hScroll = GetNewControl(257, myWindow); /* horizontal scrollbar, not shown */
  90.    SetRect(&growBoxRect, (**vScroll).contrlRect.left+1,
  91.     (**vScroll).contrlRect.bottom+1, myWindow->portRect.right,
  92.     myWindow->portRect.bottom); /* This area is set up for ValidRect, below. */
  93.  
  94.    CheckItem(hdlScrollMenu, Vertical, true);
  95.    CheckItem(hdlScrollMenu, TextItem, true);
  96. }   /* end of SetUpData */
  97.  
  98.  
  99. GrafUpdate(whatpart) /* ---------------------------------------------------------- */
  100.    Rect whatpart;
  101. /* This is roughly the equivalent of what TEUpdate does with text.  The upper left
  102. hand corner of the page is moved up and to the left to simulate a view further down
  103. and to the right.  To more accurately resemble a Toolbox routine like TEUpdate, this
  104. procedure should also preserve the current clip region and origin. */
  105. {
  106.    short count;
  107.    Rect dummyRect;
  108.  
  109.    SetOrigin(pageCorner.h, pageCorner.v); /* negative moves the origin left, up */
  110.    OffsetRect(&whatpart, pageCorner.h, pageCorner.v); /* also move update rect */
  111.    ClipRect(&whatpart); /* only redraw the portion that the user requests */
  112.    for (count = 0; count < NumOfRects; ++count)
  113. /* Redraw the object if it intersects the update rectangle */
  114.       if (SectRect(&(**myRect)[count], &whatpart, &dummyRect))
  115.          FrameRect(&(**myRect)[count]);
  116.    SetOrigin(0,0); /* reset the origin back to the upper left hand corner */
  117.    ClipRect(&myWindow->portRect); /* reset the clip region to the entire window */
  118. } /* end of GrafUpdate */
  119.  
  120.  
  121. ScrollBits() /* ------------------------------------------------------------------ */
  122. /* This routine scrolls horizontally and vertically both graphics and text.  If you
  123. are only scrolling text, only the TEScroll is required.  If you are only scrolling
  124. graphics, then only the ScrollRect and GrafUpDate is needed. */
  125.  
  126. {
  127.    short     vChange,
  128.              hChange,
  129.              vScrollValue,
  130.              hScrollValue;
  131.    RgnHandle anUpdateRgn;
  132.  
  133.    vScrollValue = GetCtlValue(vScroll); /* These values will be used a lot so they */
  134.    hScrollValue = GetCtlValue(hScroll); /* are read into temporary variables. */
  135.  
  136. /* Find the vertical and horizontal change. */
  137.    vChange = pageCorner.v - vScrollValue; /* the vertical difference */
  138.    hChange = pageCorner.h - hScrollValue; /* the horizontal difference */
  139.  
  140. /* Record the values for next time */
  141.    pageCorner.v = vScrollValue;
  142.    pageCorner.h = hScrollValue;
  143.  
  144. /* For pure text, only a TEScroll is required. */
  145.    if (!showGraphics && showText) TEScroll(hChange, vChange, hTE);
  146.  
  147. /* For graphics, a ScrollRect will move the visible bits on the screen.  The
  148.   region returned by ScrollRect indicates what part of the window needs to
  149.   be updated. */
  150.    if (showGraphics) {
  151.       anUpdateRgn = NewRgn();
  152.       ScrollRect(&myViewRect, hChange, vChange, anUpdateRgn);
  153.  
  154. /* This draws the new text.  The clipping is necessary because normally
  155.   TextEdit redraws the entire character height and perhaps only a partial
  156.   character scroll was done.  Since TextEdit erases before it draws, the text,
  157.   if any, is drawn before the graphics. */
  158.       if (showText) {
  159.          (**hTE).destRect.left = -hScrollValue;
  160.          (**hTE).destRect.top = -vScrollValue;
  161.          ClipRect(&(**anUpdateRgn).rgnBBox);
  162.          TEUpdate(&(**anUpdateRgn).rgnBBox, hTE);
  163.          ClipRect(&(myWindow->portRect));
  164.       } /* end of showText */
  165.  
  166.       GrafUpdate((**anUpdateRgn).rgnBBox); /* Fill in the newly exposed region */
  167.       DisposeRgn(anUpdateRgn);
  168.    } /* end of showGraphics */
  169. } /* end of ScrollBits */
  170.  
  171. pascal void TrackScroll(theControl, partCode) /*---------------------------------- */
  172.    ControlHandle    theControl;
  173.    short            partCode;
  174. /*This routine adjusts the value of the scrollbar.  A reasonable change would
  175. be to adjust the minimum scroll amount to equal the text's lineheight. */
  176. {
  177.    short            amount,
  178.                     startValue;
  179.    Boolean          up;
  180.  
  181.    up = (partCode == inUpButton) || (partCode == inPageUp); /*true if scrolling up */
  182.    startValue = GetCtlValue(theControl);  /*the initial control value */
  183.  
  184.    if /*the scrollbar value is decreased, and it is not already at the minimum */
  185.     ( ((up && (startValue > GetCtlMin(theControl)))
  186.     || /*the scrollbar value is increased, and it is not already at the maximum */
  187.     ((! up) && (startValue < GetCtlMax(theControl))))
  188.     && /* to prevent tracking if the page up or down area disappears */
  189.     (originalPart == partCode) ) {
  190.       if (up)
  191.          amount = -1;
  192.       else
  193.          amount = 1;  /* Set the direction. */
  194.       if ((partCode == inPageUp) || (partCode == inPageDown)) {
  195. /* Change the movement to a full page. */
  196.          if (theControl == vScroll)
  197.             amount *= myViewRect.bottom - myViewRect.top;
  198.          else
  199.             amount *= myViewRect.right - myViewRect.left;
  200.       } /* end of partCode */
  201.       SetCtlValue(theControl, startValue + amount);
  202.       ScrollBits();
  203.    }
  204. } /* end of TrackScroll */
  205.  
  206. MyControls() /* ------------------------------------------------------------------ */
  207. /* respond to a mouse down event in one of the controls */
  208. {
  209.    short          dummy;
  210.    ControlHandle  theControl;
  211.  
  212. /* return control and part */
  213.    originalPart = FindControl(&eventPoint, myWindow, &theControl);
  214.    if (originalPart == inThumb) {
  215. /* The thumb is tracked until it is released; then the bits are scrolled. */
  216.       dummy = TrackControl(theControl, &eventPoint, nil);
  217.       ScrollBits();
  218.    } /* end of whichpart */
  219. /* For the arrows and the page changes, scroll while the mouse is held down. */
  220.    else dummy = TrackControl(theControl, &eventPoint, TrackScroll);
  221. } /* end of MyControls */
  222.  
  223. MainEventLoop() /*---------------------------------------------------------------- */
  224. /* respond to menu selections, the scrollbars, and update events */
  225. {
  226.    EventRecord myEvent;                /* all of the information about the event */
  227.    int         menuResult;             /* information returned by MenuSelect */
  228.    short       theMenu,                /* which menu was selected */
  229.                theItem;                /* which item within the menu */
  230.    Boolean     checked;                /* Is the menu item checked? */
  231.    short       markChar;               /* the checkmark character */
  232.    WindowPtr   tempWindow;
  233.    Rect        tempRect;
  234.  
  235.    do {
  236.       checked = GetNextEvent(everyEvent,&myEvent); /*checked here is ignored */
  237.       switch(myEvent.what) {
  238.       case mouseDown: /* The user pressed or is holding the mouse button down. */
  239.          switch(FindWindow(&myEvent.where,&tempWindow)) {
  240.          case inMenuBar: /*the mouseDown was in the menu bar */
  241.             menuResult = MenuSelect(&myEvent.where);
  242.             theMenu = HiWord(menuResult);
  243.             theItem = LoWord(menuResult);
  244.             switch(theMenu) {
  245.                case FileMenu:
  246.                   doneFlag = true;    /* Quit  */
  247.                   break;
  248.                case ScrollMenu:
  249. /* The items in the menu are used to keep track of the user has chosen thus far.
  250. These lines toggle the checkmark in the menu and leave the result in the variable
  251. 'checked'. */
  252.                   GetItemMark(hdlScrollMenu, theItem, &markChar);
  253.                   checked = (markChar != checkMark);
  254.                   CheckItem(hdlScrollMenu, theItem, checked);
  255.  
  256. /* Any selection will cause some part of the screen to be redrawn.  The selection
  257. that the user makes causes some part of the screen to become invalid. */
  258.                   if ((theItem == TextItem) || (theItem == Graphics)) {
  259.                      InvalRect(&myViewRect);
  260. /* The small area between the scrollbars reserved for the grow box should never be
  261. redrawn. */
  262.                      ValidRect(&growBoxRect);
  263.                   }
  264.                   switch(theItem) {
  265.                   case Horizontal:
  266.                      InvalRect(&(**hScroll).contrlRect);
  267.                      if (checked) {
  268.                         ShowControl(hScroll);
  269.                         myViewRect.bottom = (**hScroll).contrlRect.top;
  270.                      } /* end of checked */
  271.                      else { /* not checked */
  272.                         HideControl(hScroll);
  273.                         myViewRect.bottom = (**hScroll).contrlRect.bottom;
  274.                      } /* end of not checked */
  275.                      break; /* end of horizontal */
  276.  
  277.                   case Vertical:
  278.                      InvalRect(&(**vScroll).contrlRect);
  279.                      if (checked) {
  280.                         ShowControl(vScroll);
  281.                         myViewRect.right = (**vScroll).contrlRect.left;
  282.                      } /* end of checked */
  283.                      else { /*not checked */
  284.                         HideControl(vScroll);
  285.                         myViewRect.right = (**vScroll).contrlRect.right;
  286.                      } /* end of not checked */
  287.                      break; /* end of vertical */
  288.  
  289.                   case TextItem:
  290. /* Since we dereference the destRect into tempRect, no calls in the scope of this
  291. statement should cause a memory compaction. */
  292.                      showText = checked;
  293.                      if (checked) {
  294.                         (**hTE).destRect.top = -GetCtlValue(vScroll);
  295.                         (**hTE).destRect.left = -GetCtlValue(hScroll);
  296.                      } /* end of checked */
  297.                      break; /* end of textItem */
  298.  
  299.                   case Graphics:
  300.                      showGraphics = checked;
  301.                      break;
  302.                   } /* end of theItem switch */
  303.                if (showText)
  304.                   (**hTE).viewRect = myViewRect;
  305.                break; /* end of case ScrollMenu */
  306.             } /* end of theMenu switch */
  307.             HiliteMenu(0);
  308.             break; /* end of case inMenuBar: */
  309.  
  310.          case inContent:
  311. /* Rectangles making up controls are the part of the window outside the 'view'. */
  312.             eventPoint = myEvent.where;
  313.             GlobalToLocal(&eventPoint);
  314.             if (!PtInRect(&eventPoint, &myViewRect))
  315.               MyControls();
  316.             break; /* end of case inContent: */
  317.          } /* end of FindWindow switch */
  318.          break; /* end of case mouseDown: */
  319.  
  320.          case updateEvt:
  321. /* In response to InvalRects, the appropriate text or graphics is erased and redrawn.
  322. The BeginUpdate causes the VisRgn to be replaced by the intersection of the VisRgn
  323. and the UpdateRgn. */
  324.             BeginUpdate(myWindow);
  325.             EraseRect(&myViewRect); /*start with a clean slate */
  326.             if (showText)
  327.                TEUpdate(&(**(myWindow->visRgn)).rgnBBox, hTE);
  328. /* Call GrafUpdate with the intersection, if any, of the VisRgn and the view */
  329.             if (showGraphics && SectRect(&(**myWindow->visRgn).rgnBBox, &myViewRect,
  330.              &tempRect))
  331.                GrafUpdate(tempRect);
  332.             EndUpdate(myWindow);
  333.             break; /* end of case updateEvent: */
  334.          } /* end of myEvent.what switch */
  335.       } /* end of do */
  336.    while(!doneFlag);
  337. } /* end of event loop */
  338.  
  339.  
  340. main() /* ------------------------------------------------------------------------ */
  341. /* main program */
  342. {
  343.    InitGraf(&qd.thePort); /* Initialize QuickDraw. */
  344.    InitWindows();         /* Initialize Window Manager; clear desktop and menubar. */
  345.    InitFonts();           /* Initialize Font Manager. */
  346.    FlushEvents(everyEvent,0); /* Throw away any stray events. */
  347.    TEInit();              /* Initialize TextEdit. */
  348.    InitMenus();           /* Initialize Menu Manager. */
  349.    hdlScrollMenu = GetMenu(FileMenu); /* (hdlScrollMenu is ignored.) */
  350.    InsertMenu(hdlScrollMenu,0);
  351.    hdlScrollMenu = GetMenu(ScrollMenu);
  352.    InsertMenu(hdlScrollMenu,0);
  353.    DrawMenuBar();
  354.    myWindow = GetNewWindow(256, nil, (WindowPtr) -1); /* get window to work within */
  355.    SetPort(myWindow);     /* Point to a window. */
  356.    TextFont(applFont);    /* Select default application font. */
  357.    SetUpData();           /* Initialize user data and controls. */
  358.    InitCursor();          /* Change the watch into an arrow. */
  359.    MainEventLoop();       /* Handle events until we are through. */
  360. }
  361.